"""
清算服务
"""

from jlpay.services.base_service import BaseService
from jlpay.model.clearing import (
    WithdrawQueryRequest, WithdrawQueryResponse,
    WithdrawApplyRequest, WithdrawApplyResponse,
    FileUploadRequest, FileUploadResponse,
    AccBalanceRequest, AccBalanceResponse,
    NotifyDto
)


class ClearingService(BaseService):
    """清算服务类"""
    
    def __init__(self, config=None):
        """
        初始化清算服务
        
        Args:
            config: OrgConfig实例，可选。如果为None，则只支持本地方法如parse_notify
        """
        if config is not None:
            super().__init__(config)
        else:
            # 如果config为None，不调用父类构造函数，只支持本地方法
            self.config = None
    
    def get_api_url(self, request_class, api_path=None):
        """
        获取API URL
        
        Args:
            request_class: 请求类
            api_path: 接口路径
            
        Returns:
            完整的API URL
        """
        if self.config is None:
            raise ValueError("ClearingService未初始化配置，无法进行HTTP请求")
            
        base_url = self.config.url
        
        if api_path:
            return f"{base_url}{api_path}"
        
        # 使用Request类中定义的API_PREFIX和API_URI
        api_prefix = getattr(request_class, 'API_PREFIX', '')
        api_uri = getattr(request_class, 'API_URI', '')
        
        if not api_uri:
            # 如果没有定义API_URI，使用默认路径
            return f"{base_url}/fund/withdraw"
        
        # 组合API路径
        path = api_prefix.lstrip('/') + '/' + api_uri.lstrip('/')
        return f"{base_url}/{path}"
    
    def query_withdraw(self, request):
        """
        查询提现结果
        
        Args:
            request: WithdrawQueryRequest实例
            
        Returns:
            WithdrawQueryResponse实例
        """
        return self.post(request, WithdrawQueryResponse)
    
    def apply_withdraw(self, request):
        """
        申请提现
        
        Args:
            request: WithdrawApplyRequest实例
            
        Returns:
            WithdrawApplyResponse实例
        """
        return self.post(request, WithdrawApplyResponse)
    
    def upload_video(self, request):
        """
        上传视频文件
        
        Args:
            request: FileUploadRequest实例
            
        Returns:
            FileUploadResponse实例
        """
        return self.post(request, FileUploadResponse)
    
    def query_acc_balance(self, request):
        """
        查询结算资金
        
        Args:
            request: AccBalanceRequest实例
            
        Returns:
            AccBalanceResponse实例
        """
        return self.post(request, AccBalanceResponse)
    
    def parse_notify(self, notify_data):
        """
        解析通知消息
        
        Args:
            notify_data: 通知数据字典
            
        Returns:
            NotifyDto实例
        """
        return NotifyDto(notify_data) 